package com.pxl.controller.student;

import com.pxl.entity.Course;
import com.pxl.entity.Institution;
import com.pxl.entity.Student;
import com.pxl.entity.Teacher;
import com.pxl.service.CourseService;
import com.pxl.service.PageService;
import com.pxl.service.StudentService;
import com.pxl.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import sun.java2d.pipe.ValidatePipe;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    @Qualifier("userServiceImpl")
    private UserService userService;

    @Autowired
    @Qualifier("courseServiceImpl")
    private CourseService courseService;

    @Autowired
    @Qualifier("pageServiceImpl")
    private PageService pageService;

    /**
     *  学生首页
     * @return
     */
    @RequestMapping("/studentIndex")
    public String studentIndex() {
        return "student/studentIndex";
    }


    /**
     *  学生详细信息
     */
    @RequestMapping("/studentInfo")
    public String studentInfo(@RequestParam("stuid")int id, Model model) {

        Student student = userService.selectStuById(id);
        model.addAttribute("studentInfo",student);

        System.out.println("........................................");
        return "student/studentInfo";
    }

    /**
     *  编辑学生密码的信息
     */
    @RequestMapping("/editStuPass")
    public String changePassword() {

        return "student/editStuPass";
    }

    @RequestMapping("/changeStuPass")
    public String changeStuPass(@RequestParam("prePass") String prePass, @RequestParam("firstPass")String nowPass, String id,Model model, HttpServletRequest request) {

        // 拿到用户的 id ,根据 id 进行修改用户的 密码
        int stuId = Integer.parseInt(id);
        System.out.println(stuId);
        System.out.println(".....................................................................");

        // 如果原密码不对,则让用户重新输入
        if(userService.checkAccount(stuId,prePass) == 0) {
            model.addAttribute("msg","原密码错误!");
            return "student/editStuPass";
        } else {
            Student student = new Student();
            student.setStuId(stuId);

            student.setStuPass(nowPass);

            userService.changeStuPass(student);
            model.addAttribute("studentInfo",userService.selectStuById(stuId));
            return "student/studentInfo";
        }

    }

    /**
     *  我的选课
     * @return
     */
    @RequestMapping("/myCourse")
    public String myCourse(Model model,HttpServletRequest request) {

        System.out.println(request.getSession().getAttribute("stuid") + "......");
        model.addAttribute("courseList",courseService.queryStuCourse((int)request.getSession().getAttribute("stuid")));

        return "/student/myCourse";
    }


    /**
     *  课程列表
     * @param model
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "/courseList",method = RequestMethod.GET)
    public String courseList(@RequestParam(value = "page",required = false) Integer page, String value, Model model, HttpServletRequest request){
        System.out.println(page);
        System.out.println(value);
        System.out.println("course");
        model.addAttribute("paging",pageService.subList(page,courseService.queryAllCourse((int)request.getSession().getAttribute("stuid"))));
        // 查询所有老师
        model.addAttribute("teaList",userService.queryAllTeacher());
        // 查询所有学生
        model.addAttribute("insList",userService.queryAllIns());
        return "student/courseList";
    }

    @RequestMapping("/pageList")
    public String pageList(@RequestParam("page") int page, String value,Model model,HttpServletRequest request){
        System.out.println("...........................................");
        System.out.println(value);
        model.addAttribute("paging",pageService.subList(page,courseService.queryAllCourse((int)request.getSession().getAttribute("stuid"))));
        // 查询所有老师
        model.addAttribute("teaList",userService.queryAllTeacher());
        // 查询所有学生
        model.addAttribute("insList",userService.queryAllIns());
        return "student/courseList";
    }

    /**
     *  通过筛选老师的姓名进行查询对应的课程
     * @param teaId
     * @param model
     * @return
     */
    @RequestMapping("/searchListByTeaId")
    public String searchListByTeaId(int teaId,Model model) {
        System.out.println(teaId);
        // 通过老师的 id 找到老师的所教的所有课程,在 service 层还需要进行和 学院表进行连接,因为还要知道老师是哪个学院的
        List<Course> cor_list = courseService.queryAllById(teaId);
        model.addAttribute("paging",pageService.subList(1,cor_list));
        model.addAttribute("teaList",userService.queryAllTeacher());
        model.addAttribute("insList",userService.queryAllIns());
        return "student/courseList";
    }

    @RequestMapping("/searchListByInsId")
    public String searchListByInsId(@RequestParam("insId") int insId,Model model) {

        List<Course> cor_list = courseService.queryAllByInsId(insId);

        model.addAttribute("paging",pageService.subList(1,cor_list));
        model.addAttribute("teaList",userService.queryAllTeacher());
        model.addAttribute("insList",userService.queryAllIns());

        return "student/courseList";
    }

    @RequestMapping("/searchCourse")
    public String searchCourse(@RequestParam("classId") int classId,Model model) {

        List<Course> list = courseService.queryCourseByClassId(classId);

        model.addAttribute("paging",pageService.subList(1,list));
        model.addAttribute("teaList",userService.queryAllTeacher());
        model.addAttribute("insList",userService.queryAllIns());

        return "student/courseList";
    }

    /**
     *  学生选课时会出现课程详情
     * @param classId
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/courseDetail")
    public String courseDetail(@RequestParam("classId") int classId,Model model,HttpServletRequest request) {
        System.out.println(classId);
        System.out.println("////////////////////////////////////////////////////////////////////////////");
        if(courseService.checkStuIns(classId,(int)request.getSession().getAttribute("stuid"))) {
            // 学院限制
            System.out.println(".........................................");
            // 查询课程详情
            model.addAttribute("course",courseService.queryCourseDetail(classId));
            // 跳转到课程详情页面
            return "student/courseDetail";
        } else {
            model.addAttribute("msg","请注意课程的学院限制!");
            model.addAttribute("paging",pageService.subList(1,courseService.queryAllCourse((int)request.getSession().getAttribute("stuid"))));
            model.addAttribute("teaList",userService.queryAllTeacher());
            model.addAttribute("insList",userService.queryAllIns());
            return "student/courseList";
        }
    }


    /**
     *  学生退课
     * @param classId
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/deleteCourse")
    public String deleteCourse(@RequestParam("classId") int classId,Model model,HttpServletRequest request) {

        // 退课
        courseService.deleteCourseChoose((int)request.getSession().getAttribute("stuid"),classId);

        int stuid = (int) request.getSession().getAttribute("stuid");

        // 因为还要得到学生的一些课程信息,所以需要将学号传到后台数据
        model.addAttribute("paging",pageService.subList(1,courseService.queryAllCourse(stuid)));
        model.addAttribute("teaList",userService.queryAllTeacher());
        model.addAttribute("stuList",userService.queryAllIns());
        return "student/courseList";
    }


    /**
     *  选择课程详情中的选择按钮
     * @param classId
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/chooseCourse")
    public String chooseCourse(@RequestParam("classId") int classId,Model model,HttpServletRequest request) {

        courseService.chooseCourse(classId, (int) request.getSession().getAttribute("stuid"));

        model.addAttribute("paging",pageService.subList(1,courseService.queryAllCourse((int) request.getSession().getAttribute("stuid"))));
        model.addAttribute("teaList",userService.queryAllTeacher());
        model.addAttribute("insList",userService.queryAllIns());

        return "student/courseList";
    }

}
